let keys = '4UTBZ-PUWLX-UGS4R-TIK3C-WFL73-N4FWH';
const QQMapWX = require('../../libs/qqmap-wx-jssdk.js');
const qqmapsdk = new QQMapWX({
  key:keys
});
Page({
  data: {
    title:'',
    address:'',
    selectIndex:0,
    scale: 18,//缩放级别
    latitude: 0,
    longitude: 0,
    chooseOk:0,
    maksHidden:false,
    changeCity:false
      },
  onLoad: function (options) {

    const that=this;
      wx.getLocation({
        success: function(res) {
        that.getDistrict(res.latitude,res.longitude)
        },
      });  
  
},
goBack:function(){
  wx.navigateBack();
},
getDistrict(latitude, longitude) {
  let that = this;
  wx.request({
    url: `https://apis.map.qq.com/ws/geocoder/v1/location=${latitude},${longitude}&key=${keys}`,
    header: {
      'Content-Type': 'application/json'
    },
    success: function(res) {
      console.log(res.data.result)     
   
      let province = res.data.result.address_component.province;      
      let city = res.data.result.address_component.city;
     // cityList,provinceList
    
     that.setData({
      province:province,
      city: city,
      //scrollTop:scrollTop
    })
   
   
    
      
   
    }
  })
},

 closeMaks:function(){
   this.setData({
      maksHidden:false
   });
 },
 
chooseAddress:function(e){
  var address=e.currentTarget.dataset.address;
  var title=e.currentTarget.dataset.title;
  let pages = getCurrentPages(); //获取当前页面js里面的pages里的所有信息。
 
  let prevPage = pages[pages.length - 2];
  prevPage.setData({
    position:title+'('+address+')',
  })
  wx.navigateBack();
   //this.dragSearch();
},



getLngLat: function(){
  var that = this;
  this.mapCtx = wx.createMapContext("myMap");
  this.mapCtx.getCenterLocation({
    success: function(res){
      console.log(res);
        that.setData({
        longitude: res.longitude,
        latitude: res.latitude
      })
      that.getAddress();
    }
  })
},
controlTap:function(e){
  console.log('control');
  console.log(e);
  
this.mapCtx.moveToLocation()

},
positionSearch:function(e){
  console.log(e);
  var _this = this;
  // 调用接口
  qqmapsdk.search({
    keyword: e.detail.value,  //搜索关键词
    location: _this.data.latitude + ',' + _this.data.longitude,  //设置周边搜索中心点
    success: function (res) { //搜索成功后的回调
    console.log(res);
    
      var mks = []
     if(res.data){
      for (var i = 0; i < res.data.length; i++) {
        mks.push({ // 获取返回结果，放到mks数组中
          title: res.data[i].title,
          address:res.data[i].address,
          id: res.data[i].id,
          city:res.data[i].ad_info.city,
          district:res.data[i].ad_info.district,
          province:res.data[i].ad_info.province,
          latitude: res.data[i].location.lat,
          longitude: res.data[i].location.lng,
           iconPath: '/icons/position.png', //图标路径, //图标路径
          width: 20,
          height: 20
        })
        if (i==0) {
          _this.data.title = res.data[i].title;
          _this.data.address = res.data[i].address;
        }
      }
    }
     
      _this.setData({ //设置markers属性，将搜索结果显示在地图中
        markers: mks
      })
    },
    fail: function (res) {
      console.log(res);
    },
    complete: function (res) {
      console.log(res);
    }
  });
},

onReady:function(){
  this.mapCtx=wx.createMapContext("myMap");
  //this.mapCtx.getCenterLocation({
},
onShow: function () {
    var that = this;
    
    //基础定位
    wx.getLocation({
      type: 'gcj02',
       success: function (res) {
        console.log(res)
        var latitude1 = res.latitude
        var longitude1 = res.longitude
          that.setData({
          latitude: res.latitude,
          longitude: res.longitude,
          })
         that.getAddress();
          
    
      }
    })
  },
  getAddress:function(){
    let that=this;
    qqmapsdk.reverseGeocoder({
      location: {
        latitude: that.data.latitude,
        longitude: that.data.longitude
      },
      get_poi:1,
      poi_options:'radius=5000',
      success: function (res) {
        console.log(res);
        var res = res.result;
        var mks = [];
       
       for (var i = 0; i < res.pois.length; i++) {
         mks.push({ // 获取返回结果，放到mks数组中
             title: res.pois[i].title,
             address: res.pois[i].address,
             id: res.pois[i].id,
             city:res.pois[i].ad_info.city,
             province:res.pois[i].ad_info.province,
             district:res.pois[i].ad_info.district,
             latitude: res.pois[i].location.lat,
             longitude: res.pois[i].location.lng,           
             width: 20,
             height: 20
         })
         if(i==0){
           that.data.title = res.pois[i].title;
           that.data.address = res.pois[i].address;
         }
       }
       that.setData({
         markers: mks
       })
      },
      fail:function(e){
        console.log(e);

      }
    })
  }
         
})

